<template>
  <div class="login-container">
    <div v-if="loading" class="login-loading">
      <el-icon class="loading-icon"><LoadingIcon /></el-icon>
      <span>登录模块加载中...</span>
    </div>
    
    <div v-else class="login-box">
      <div class="logo-container">
        <img src="@/assets/logo.svg" alt="项目Logo" class="logo">
        <h2 class="project-title">项目管理</h2>
      </div>
      
      <el-form 
        :model="form" 
        :rules="rules"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="user"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="form.remember">记住密码</el-checkbox>
        </el-form-item>
        
        <el-button
          type="primary"
          native-type="submit"
          :loading="loginLoading"
          style="width: 100%"
        >
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Loading as LoadingIcon } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

export default {
  name: 'Login',
  setup() {
    const loading = ref(true)
    const loginLoading = ref(false)
    const form = reactive({
      username: '',
      password: '',
      remember: false
    })
    const rules = reactive({
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
    })
    const router = useRouter()
    const userStore = useUserStore()

    // 模拟组件加载
    setTimeout(() => {
      loading.value = false
      // 初始化记住的用户名
      const rememberedUsername = localStorage.getItem('rememberedUsername')
      if (rememberedUsername) {
        form.username = rememberedUsername
        form.remember = true
      }
    }, 300)

    const handleLogin = async () => {
      loginLoading.value = true
      try {
        // Mock用户验证
        if (form.username === 'admin' && form.password === '123456') {
          await userStore.login({
            username: form.username,
            password: form.password
          })
          
          // 记住密码功能
          if (form.remember) {
            localStorage.setItem('rememberedUsername', form.username)
          } else {
            localStorage.removeItem('rememberedUsername')
          }
          
          ElMessage.success('登录成功')
          router.push('/')
        } else {
          throw new Error('用户名或密码错误')
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loginLoading.value = false
      }
    }

    return {
      loading,
      loginLoading,
      form,
      rules,
      handleLogin,
      Loading: LoadingIcon
    }
  }
}
</script>

<style>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  flex-direction: column;
}

.login-box {
  width: 420px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin: 0 auto;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

.el-form-item__label {
  width: 80px;
  text-align: right;
  padding-right: 12px;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 30px;
  gap: 20px;
}

.logo {
  width: 60px;
  height: 60px;
}

.project-title {
  margin: 0;
  font-size: 24px;
  color: #333;
  font-weight: bold;
}

.login-box .el-form-item {
  margin-bottom: 24px;
}

.login-box .el-input {
  width: 100%;
}

.login-box .el-button {
  margin-top: 16px;
}

.loading-icon {
  font-size: 40px;
  margin-bottom: 20px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>